﻿<UserControl x:Class="Squiggle.UI.Controls.ContactListItem"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:converters="clr-namespace:Squiggle.UI.Converters"
             xmlns:helpers="clr-namespace:Squiggle.UI.Helpers"    
             xmlns:local="clr-namespace:Squiggle.UI.Controls"
             xmlns:chat="clr-namespace:Squiggle.Client;assembly=Squiggle.Client"
             xmlns:scm="clr-namespace:System.ComponentModel;assembly=WindowsBase" 
             xmlns:dragdrop="clr-namespace:FluidKit.Helpers.DragDrop;assembly=FluidKit"     
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <UserControl.Resources>
        <converters:StatusIconConverter x:Key="statusIconConverter" />
        <converters:StatusConverter x:Key="statusConverter" />
        <converters:ORMultiConverter x:Key="orConverter" />
        <local:ChatDragDropAdvisor x:Key="contactDragDrop" />
        <Style x:Key="StatusSymbol" TargetType="{x:Type Ellipse}">
            <Setter Property="Width" Value="9"/>
            <Setter Property="Height" Value="9"/>
            <Setter Property="Margin" Value="10,0,10, 0"/>
            <Setter Property="Fill" Value="#FF7DB96D"/>
            <Style.Triggers>
                <DataTrigger Binding="{Binding Status}" Value="Online">
                    <Setter  Property="Fill" Value="#FF7DB96D" />
                </DataTrigger>
                <DataTrigger Binding="{Binding Status}" Value="Offline">
                    <Setter Property="Fill" Value="#FFCFCFCF" />
                </DataTrigger>
                <DataTrigger Binding="{Binding Status}" Value="Busy">
                    <Setter Property="Fill" Value="#FFEB6D6E" />
                </DataTrigger>
                <DataTrigger Binding="{Binding Status}" Value="Away">
                    <Setter Property="Fill" Value="#FFFFB937" />
                </DataTrigger>
                <DataTrigger Binding="{Binding Status}" Value="Idle">
                    <Setter Property="Fill" Value="#FFFFB937" />
                </DataTrigger>
                <DataTrigger Binding="{Binding Status}" Value="BeRightBack">
                    <Setter Property="Fill" Value="#FFFFB937" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
        <DataTemplate x:Key="standardView">
            <Border x:Name="contactBorder" Padding="1" Cursor="Hand"
                    dragdrop:DragDropManager.DragSourceAdvisor="{DynamicResource contactDragDrop}">
                <Border.ToolTip>
                    <ToolTip Style="{StaticResource BuddyToolTip}" />
                </Border.ToolTip>
                <Grid Margin="5">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="30"/>
                        <ColumnDefinition Width="*"/>
                        <ColumnDefinition Width="30"/>
                    </Grid.ColumnDefinitions>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition Height="0.01*"/>
                    </Grid.RowDefinitions>
                    <Image Width="20" Height="20" Source="{Binding Path=Status, Converter={StaticResource statusIconConverter}}" VerticalAlignment="Top"/>
                    <StackPanel Orientation="Vertical" Grid.Column="1">
                        <StackPanel HorizontalAlignment="Left" Grid.Column="1"
                                            Orientation="Horizontal" Margin="5,0,0,0" Cursor="Hand" >
                            <TextBlock Text="{Binding Path=DisplayName}" />
                            <TextBlock x:Name="buddyStatus" Margin="5,0,0,0" Text="{Binding Path=Status, Converter={StaticResource statusConverter}, StringFormat={}({0})}" />
                        </StackPanel>
                        <TextBlock Margin="5,0,0,0" Text="{Binding Path=Properties.DisplayMessage}" TextTrimming="WordEllipsis" ToolTip="{Binding Path=DisplayMessage}"  FontSize="10" Foreground="#FF9A9C9D" HorizontalAlignment="Left" />
                    </StackPanel>
                    <Image Width="30" Height="30" Stretch="UniformToFill" Source="{Binding Properties.DisplayImage}" VerticalAlignment="Top" Grid.Column="2" />
                </Grid>
            </Border>
            <DataTemplate.Triggers>
                <DataTrigger Binding="{Binding Status}" Value="Online">
                    <Setter TargetName="buddyStatus" Property="Visibility" Value="Hidden" />
                </DataTrigger>
                <DataTrigger Binding="{Binding Status}" Value="Offline">
                    <Setter TargetName="buddyStatus" Property="Visibility" Value="Hidden" />
                </DataTrigger>
                <Trigger Property="ItemsControl.AlternationIndex" Value="0">
                    <Setter Property="Background" Value="White" 
                                TargetName="contactBorder"></Setter>
                </Trigger>
                <Trigger Property="ItemsControl.AlternationIndex" Value="1">
                    <Setter Property="Background" Value="WhiteSmoke" 
                                TargetName="contactBorder"></Setter>
                </Trigger>
                <Trigger SourceName="contactBorder" Property="IsMouseOver" Value="True">
                    <Setter TargetName="contactBorder" Property="Background" Value="{StaticResource ContactBorderBrush}" />                    
                </Trigger>
            </DataTemplate.Triggers>
        </DataTemplate>
        <DataTemplate x:Key="compactView">
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*" />
                </Grid.ColumnDefinitions>
                <Border x:Name="contactBorder" Cursor="Hand"  Height="20" 
                        dragdrop:DragDropManager.DragSourceAdvisor="{DynamicResource contactDragDrop}">
                    <StackPanel Orientation="Horizontal" Cursor="Hand" VerticalAlignment="Center" Margin="10,0,0,0" >
                        <Ellipse x:Name="ellipseStatus" Style="{StaticResource StatusSymbol}" />
                        <TextBlock Text="{Binding Path=DisplayName}" FontSize="11" />
                        <TextBlock Margin="5,0,0,0" Text="{Binding Path=Properties.DisplayMessage}" TextTrimming="WordEllipsis" ToolTip="{Binding Path=DisplayMessage}"  FontSize="10" Foreground="#FF9A9C9D" HorizontalAlignment="Left" />
                    </StackPanel>
                </Border>
                <Popup x:Name="popup" PlacementTarget="{Binding ElementName=contactBorder}"
                           VerticalOffset="0"  Placement="Left" Width="350" Height="170"
                           AllowsTransparency="true" Focusable="true" StaysOpen="true">
                    <Popup.IsOpen>
                        <MultiBinding Converter="{StaticResource orConverter}" Mode="OneWay">
                            <Binding ElementName="contactBorder" Path="IsMouseOver" Mode="OneWay" />
                            <Binding ElementName="popup" Path="IsMouseOver" Mode="OneWay" />
                        </MultiBinding>
                    </Popup.IsOpen>
                    <Border Margin="5" BorderBrush="Black" BorderThickness="1">
                        <Grid>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="Auto"/>
                                <RowDefinition Height="*"/>
                                <RowDefinition Height="Auto"/>
                            </Grid.RowDefinitions>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="0.3*"/>
                                <ColumnDefinition Width="0.7*"/>
                            </Grid.ColumnDefinitions>
                            <DockPanel Background="#FFD6D9E5" Grid.ColumnSpan="2">
                                <Button DockPanel.Dock="Left" Background="White" Content="{StaticResource ContactListItem_ChatButton}" 
                                        Command="{x:Static local:ContactListItem.ChatCommand}" CommandParameter="{Binding}"
                                        MinWidth="75" Margin="5" />
                                <Button DockPanel.Dock="Left" Background="White" Content="{StaticResource ContactListItem_EmailButton}" 
                                        Command="{x:Static local:ContactListItem.EmailCommand}"  CommandParameter="{Binding}"
                                        MinWidth="75" Margin="5" />
                                <Button DockPanel.Dock="Right" Background="White" Content="{StaticResource ContactListItem_SendFileButton}" 
                                        Command="{x:Static local:ContactListItem.SendFileCommand}"  CommandParameter="{Binding}"
                                        Width="100" Margin="5" HorizontalAlignment="Right" />
                            </DockPanel>
                            <Border BorderBrush="Black" BorderThickness="0,1,0,1" Background="White" VerticalAlignment="Stretch"  Grid.ColumnSpan="2" Grid.Row="1">
                                <Grid Margin="0,5,0,0">
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="*"/>
                                        <ColumnDefinition Width="Auto"/>
                                    </Grid.ColumnDefinitions>
                                    <Grid.RowDefinitions>
                                        <RowDefinition Height="Auto"/>
                                        <RowDefinition Height="*"/>
                                    </Grid.RowDefinitions>
                                    <StackPanel Orientation="Horizontal">
                                        <Ellipse Style="{StaticResource StatusSymbol}"  Margin="5,0,5,0"  />
                                        <TextBlock Foreground="Black" FontWeight="Bold"  Text="{Binding DisplayName}"/>
                                        <TextBlock Foreground="Black" Margin="5,0,0,0" FontSize="10" Text="{Binding Status, StringFormat=(\{0\})}"/>
                                    </StackPanel>
                                    <Image Grid.Column="1" Grid.RowSpan="2" Source="{Binding Properties.DisplayImage}" Margin="5" Stretch="UniformToFill" Height="100" Width="100" />
                                    <ScrollViewer Grid.Row="1" DockPanel.Dock="Left" VerticalScrollBarVisibility="Auto" >
                                        <TextBox Margin="10,0,0,0" Foreground="#FF9A9C9D" IsReadOnly="True" BorderThickness="0" Text="{Binding Properties.DisplayMessage}" TextWrapping="WrapWithOverflow"/>
                                    </ScrollViewer>
                                </Grid>
                            </Border>
                            <DockPanel Background="White" Grid.Row="2" Grid.ColumnSpan="2" >
                                <TextBlock Margin="5,0,0,0" FontSize="10" Foreground="Black" Text="{Binding Properties.MachineName, StringFormat={StaticResource BuddyProps_MachineName}}" />
                                <TextBlock Margin="0,0,5,0" DockPanel.Dock="Right" HorizontalAlignment="Right" Foreground="Black" FontSize="10" Text="{Binding LastUpdated, StringFormat={StaticResource BuddyProps_LastUpdated}}" />
                            </DockPanel>
                        </Grid>
                    </Border>
                </Popup>
            </Grid>
            <DataTemplate.Triggers>
                <Trigger Property="ItemsControl.AlternationIndex" Value="0">
                    <Setter Property="Background" Value="White" 
                                TargetName="contactBorder"></Setter>
                </Trigger>
                <Trigger Property="ItemsControl.AlternationIndex" Value="1">
                    <Setter Property="Background" Value="WhiteSmoke" 
                                TargetName="contactBorder"></Setter>
                </Trigger>
                <Trigger SourceName="contactBorder" Property="IsMouseOver" Value="True">
                    <Setter TargetName="contactBorder" Property="Background" Value="{StaticResource ContactBorderBrush}" />                    
                </Trigger>
            </DataTemplate.Triggers>
        </DataTemplate>
        <local:ContactsViewSelector x:Key="contactViewSelector" StandardView="{StaticResource standardView}" CompactView="{StaticResource compactView}"/>
    </UserControl.Resources>
    <ContentControl ContentTemplateSelector="{StaticResource contactViewSelector}" Content="{Binding}">
    </ContentControl>
</UserControl>